package com.hospital.demo.controller.lxy;



import com.hospital.demo.mapper.lxy.DepartmentMapper;
import com.hospital.demo.mapper.lxy.PatientsMapper;
import com.hospital.demo.mapper.lxy.RegistrationMapper;
import com.hospital.demo.mapper.lxy.WorkersMapper;
import com.hospital.demo.model.lxy.Patients;
import com.hospital.demo.model.lxy.Registration;
import com.hospital.demo.model.lxy.Workers;
import com.hospital.demo.service.lxy.RegistrationService;
import com.hospital.demo.util.ResultUtils;
import com.hospital.demo.util.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 李欣远
 * @since 2020-04-08
 */
@RestController
@RequestMapping("/hospital/registration")
public class RegistrationController {
    @Autowired
    PatientsMapper patientsMapper;
    @Autowired
    private RegistrationService registrationService;
    @Autowired
    private RegistrationMapper registrationMapper;
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private WorkersMapper workersMapper;
    @Autowired
    JavaMailSenderImpl mailSender;
    @RequestMapping("/regs")
    public ResultVO list(Model model){
        List<Registration> reg= registrationService.selReg();
        model.addAttribute("regs",reg);
        return ResultUtils.success(reg);
    }
    @RequestMapping("/regs0")
    public ResultVO list0(Model model){
        List<Registration> reg= registrationService.selReg0();
        model.addAttribute("regs",reg);
        return ResultUtils.success(reg);
    }
    @GetMapping("/searchReg")
    public ResultVO searchReg(Integer pid) {
        Registration registration = new Registration();
        registration.setRpid(pid);

        List<Registration> list = registrationService.selRegByP(pid);
        return ResultUtils.success(list);
    }
    @GetMapping("/searchRegName")
    public ResultVO searchRegName(String pname) {
        Registration registration = new Registration();

        List<Registration> list = registrationMapper.selRegByPName(pname);
        return ResultUtils.success(list);
    }
    @GetMapping("/searchRegByD")
    public ResultVO searchRegByD(Integer did) {
        Registration registration = new Registration();
        registration.setRwid(did);

        List<Registration> list = registrationService.selRegByDoc(did);
        return ResultUtils.success(list);
    }
    @GetMapping("/searchRegByDe")
    public ResultVO searchRegByDe(Integer deid) {
        Registration registration = new Registration();
        registration.setRdeid(deid);

        List<Registration> list = registrationService.selRegByDe(deid);
        return ResultUtils.success(list);
    }
//    @RequestMapping("/regs/{did}")
//    public List<Registration> listD(@PathVariable("did")Integer did,Model model){
//        List<Registration> regs= registrationService.selRegByDoc(did);
//        model.addAttribute("regs",regs);
//        return regs;
//    }
//    @RequestMapping("/regs/{deid}")
//    public List<Registration> listDe(@PathVariable("deid")Integer deid,Model model){
//        List<Registration> regs= registrationService.selRegByDe(deid);
//        model.addAttribute("regs",regs);
//        return regs;
//    }
//    @RequestMapping("/regs/{pid}")
//    public List<Registration> listP(@PathVariable("pid")Integer pid,Model model){
//        List<Registration> regs= registrationService.selRegByP(pid);
//        model.addAttribute("regs",regs);
//        return regs;
//    }
//    @GetMapping("/regde")
//    public List<Department> toAddpageDe(Model model){
//        QueryWrapper<Department> wrapper = new QueryWrapper<>();
//        wrapper.select("dname");
//        List<Department> departments=departmentMapper.selectList(wrapper);
//        model.addAttribute("departments",departments);
//        return departments;
//    }
//    @GetMapping("/regd")
//    public List<Workers> toAddpageDo(Model model){
//        QueryWrapper<Workers> wrapper = new QueryWrapper<>();
//        wrapper.select("dname");
//        List<Workers> workers=workersMapper.selectList(wrapper);
//        model.addAttribute("workers",workers);
//        return workers;
//    }
    @PostMapping("/reg")
    public ResultVO addReg(Registration registration){
//        registrationService.save(registration);


        registration.setRhname("人民医院");
        Date date = new Date();
        registration.setRdate(date);
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setSubject("新挂号单");
        mailMessage.setText("有新的挂号单，请注意查看");

        mailMessage.setTo("3597945105@qq.com");
        mailMessage.setFrom("3597945105@qq.com");
        mailSender.send(mailMessage);
        return ResultUtils.success(registrationService.save(registration));
    }
//    @GetMapping("/test/{rid}")
//    public String toUpdateReg(@PathVariable("rid")Integer rid, Model model){
//        Registration registrationById=registrationService.getById(rid);
//        model.addAttribute("registrationById",registrationById);
//        QueryWrapper<Department> wrapper1 = new QueryWrapper<>();
//        wrapper1.select("dname");
//        List<Department> departments=departmentMapper.selectList(wrapper1);
//        model.addAttribute("departments",departments);
//        QueryWrapper<Workers> wrapper2 = new QueryWrapper<>();
//        wrapper2.select("dname");
//        List<Workers> workers=workersMapper.selectList(wrapper2);
//        model.addAttribute("workers",workers);
//        return "test/update";
//    }
    @PostMapping("/updateReg")
    public ResultVO updateReg(Registration registration){
//        registrationService.updateById(registration);
        System.out.println(registration);
        Patients patients = patientsMapper.selectById(registration.getRpid());
        Workers workers = workersMapper.selectById(registration.getRwid());
        String pmail= patients.getPemail();
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setSubject("挂号单更新通知");
        mailMessage.setText(patients.getPname()+"您好，您的挂号单"+registration.getRid()+"号已处理，负责您的医生为"+workers.getDname()+"请注意查看");

        mailMessage.setTo(pmail);
        mailMessage.setFrom("3597945105@qq.com");
        mailSender.send(mailMessage);
        return ResultUtils.success(registrationService.updateById(registration));
    }
    @PostMapping("/delReg")
    public ResultVO deleteReg(@RequestParam(name = "rid", defaultValue = "-1")Integer rid){
//        registrationService.removeById(rid);
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setSubject("挂号单变动");
        mailMessage.setText(rid+"号挂号单被患者撤销，请注意查看");

        mailMessage.setTo("3597945105@qq.com");
        mailMessage.setFrom("3597945105@qq.com");
        mailSender.send(mailMessage);
        Long id= Long.valueOf(rid);
        return ResultUtils.success(registrationService.removeById(id));
    }
    @PostMapping("/delRegByD")
    public ResultVO deleteRegByD(@RequestParam(name = "rid", defaultValue = "-1")Integer rid){
//        registrationService.removeById(rid);
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setSubject("挂号单变动");
        mailMessage.setText(rid+"号挂号单被医生退掉，请注意查看");

        mailMessage.setTo("3597945105@qq.com");
        mailMessage.setFrom("3597945105@qq.com");
        mailSender.send(mailMessage);
        Long id= Long.valueOf(rid);
        return ResultUtils.success(registrationService.removeById(id));
    }
}

